//
//  SLHelper.swift
//  sulit-loan
//
//  Created by 王刚锋 on 2025/4/4.
//  Copyright © 2025 coolBoy. All rights reserved.
//

import UIKit
import CoreLocation
import AdSupport
import AppTrackingTransparency
//保存
func __setValue(value:Any?,key:String) -> Void {
    if let saveValue = value {
        UserDefaults.standard.setValue(saveValue, forKey: key)
        UserDefaults.standard.synchronize()
    }
}
//获取
func __valueForKey(key:String) -> Any? {
    return  UserDefaults.standard.value(forKey: key)
}
//删除
func __removeForKey(key:String) -> Void {
    UserDefaults.standard.removeObject(forKey: key)
    UserDefaults.standard.synchronize()
}


func IMAGE(_ name:String) -> UIImage {
    if let image = UIImage(named: name){
        return image
    }
    return UIImage()
}
/// 获取当前 秒级 时间戳 - 10位
func _date_timestamp_string()->String {
    let timeInterval: TimeInterval = NSDate().timeIntervalSince1970
    let timeStamp = Int(timeInterval)
    return "\(timeStamp)"
    
}
func _date_date_string(date: Date, format: String = "yyyy-MM-dd") -> String {
    let formatter = DateFormatter.init()
    formatter.locale =   Locale.current
    formatter.dateFormat = format
    let dateString = formatter.string(from: date)
    
    return dateString
}
/// 加载xib
/// - Parameter xibName:名字
/// - Returns:
func kLoadXib(xibName:String) ->UIView{
    let obj = Bundle.main.loadNibNamed(xibName, owner: nil, options: nil)?.first
    if let view = obj as? UIView {
        return view
    }else{
        return UIView()
    }
    
}
func rgba(_ r:CGFloat,_ g:CGFloat,_ b:CGFloat,_ a:CGFloat = 1)->UIColor{
    return UIColor(red: _valueToFloat(r), green: _valueToFloat(g), blue: _valueToFloat(b), alpha: a)
}
func _valueToFloat(_ value:CGFloat)->CGFloat{
    return value/255.0
}
func rgba(hex:String,a:CGFloat = 1)->UIColor{
    
    var cstr = hex.trimmingCharacters(in:  CharacterSet.whitespacesAndNewlines).uppercased() as NSString;
    if(cstr.length < 6){
        return UIColor.clear;
    }
    if(cstr.hasPrefix("0X")){
        cstr = cstr.substring(from: 2) as NSString
    }
    if(cstr.hasPrefix("#")){
        cstr = cstr.substring(from: 1) as NSString
    }
    if(cstr.length != 6){
        return UIColor.clear;
    }
    var range = NSRange.init()
    range.location = 0
    range.length = 2
    //r
    let rStr = cstr.substring(with: range);
    //g
    range.location = 2;
    let gStr = cstr.substring(with: range)
    //b
    range.location = 4;
    let bStr = cstr.substring(with: range)
    var r :UInt32 = 0x0;
    var g :UInt32 = 0x0;
    var b :UInt32 = 0x0;
    Scanner.init(string: rStr).scanHexInt32(&r);
    Scanner.init(string: gStr).scanHexInt32(&g);
    Scanner.init(string: bStr).scanHexInt32(&b);
    return UIColor.init(red: CGFloat(r)/255.0, green: CGFloat(g)/255.0, blue: CGFloat(b)/255.0, alpha: a);
}

